package com.aline.seo.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName SysUser
 * @Description
 * @Author 孟志浩
 * @Date 2019/4/9 14:48
 **/
@Component
public class SysUser implements UserDetails {

    private Long id;
    private String username;
    private String password;
    private boolean enable;
    private boolean locked;
    private List<String> roles;

    public SysUser() {}

    public SysUser(User user, List<String> roles) {
        this.id = user.getId();
        this.locked = user.getLocked() == 1;
        this.username = user.getUsername();
        this.password = user.getPassword();
        this.enable = user.getEnable() != 0;
        this.roles = roles;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        roles.forEach((e)->{
            authorities.add(new SimpleGrantedAuthority(e));
        });
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !locked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return enable;
    }
}
